<?php
class homeActions extends BaseAdminActions
{
    public  $home;
    public $system;
    public function __construct(){        
        
        
        parent::__construct();
        Files::factory('System');

		$this->system = new System();
    }
    /**
     * 
     * 首页
     */
    public function actionsIndex()
    {        

    }
    public function actionsList()
    {
        
    }
    /**
     * 
     * 用户管理
     */
    public function actionsUser()
    {
        
        $rs = $this->userModel->getUserList($this->user);
        $this->assign('rs', $rs);
    }
    /**
     * 
     * 权限设置
     */
    public function actionsRoleset()
    {
        //Files::factory('AdminUser');
        //$user = new AdminUser();
        $uid = Http::get('id');
        if (Http::get('do')=='save'){
            $menuids = Http::get('role');
            if ($this->home->saveRole($uid,$menuids)){
                echo json_encode(array('status'=>1, 'html'=>'设置成功！'));
            }else {
                echo json_encode(array('status'=>0,'html'=>'设置失败！'));
            }
            die();
        }
        
        $menu = $this->home->getMenuList($uid);
        $this->assign('menu', $menu);
    }
    /**
     * 
     * 添加用户
     */
    public function actionsRoleadduser()
    {
        if (Http::get('do')=='add')
        {
            $username = Http::get('username');
            $password = Http::get('password');
            $isadmin = Http::get('isadmin');
            $email = Http::get('email');
            $tel = Http::get('tel');
            //$plantform_id = Http::get('plantform_id');
            if ($this->userModel->getUserByUsername($username)){
                die(json_encode(array('status'=>0,'html'=>'添加失败，用户名重复！')));
            }
        
            if ($this->userModel->getUserByEmail($email))
            {
                die(json_encode(array('status'=>0,'html'=>'添加失败，邮箱重复！')));
            }
            $data = array(
            'username'=>$username,
            'password'=>md5(md5($password).$username),
            'isadmin'=>$isadmin,
            'email'=>$email,
            //'plantform_id'=>$plantform_id,
            'tel'=>$tel
            );
            
            $uid = $this->home->addUser($data);
            
            if($uid){
                $this->home->addRoleMenu($uid);
                die(json_encode(array('status'=>1,'id'=>$uid, 'html'=>'添加成功！')));
            }else {
                die(json_encode(array('status'=>0,'html'=>'添加失败，用户名或邮箱重复！')));
            }
        }
        //$plantform = $this->home->getPlantform();
        //$this->assign('plantform', $plantform);
    }
    public function actionsModifyuser()
    {
        $uid = Http::get('id');
        if (Http::get('do')=='update')
        {
            $username = Http::get('username');
            $password = Http::get('password');
            $isadmin = Http::get('isadmin');
            $email = Http::get('email');
            $tel = Http::get('tel');
            //$plantform_id = Http::get('plantform_id');
            
            
            $data = array(
            'username'=>$username,            
            'isadmin'=>$isadmin,
            'email'=>$email,
            //'plantform_id'=>$plantform_id,
            'tel'=>$tel
            );
            if ($password!=''){
                $data['password'] = md5(md5($password).$username);
            }
            if($this->home->updateUser($data,$uid)){            
                die(json_encode(array('status'=>1,'id'=>$uid ,'html'=>'设置成功！')));
            }else {
                die(json_encode(array('status'=>0,'html'=>'设置失败，系统错误！')));
            }
        }
        $row = $this->userModel->getUserByUid($uid);
        //$plantform = $this->home->getPlantform();
        //$this->assign('plantform', $plantform);
        $this->assign('row', $row);
//        print_r($this->user);
        $this->display('roleadduser.tpl.php');
    }
    /**
     * 
     * 删除用户
     */
    public function actionsRoledel()
    {
        
        if (Http::get('id') == $this->user['uid'])
        {
            echo '删除失败,不能删除自己！！';
            die();
        }
        if($this->home->delUser(Http::get('id')))
        {
            echo '删除成功！';
        }else{
            echo '删除失败！';
        }
        die();
    }
    
    public function actionsSetpwd()
    {
        $uid = $this->user['uid'];
        $row = $this->userModel->getUserByUid($uid);
        //$plantform = $this->home->getPlantform();
        //$this->assign('plantform', $plantform);
        $this->assign('row', $row);
        $this->display('roleadduser.tpl.php');
    }
    
    public function actionsSetmenu()
    {
        if (http::get('do')=='del'){
            $sql = "DELETE FROM ".$this->home->menutable." WHERE id = ".Http::get('id');
            if($this->home->db->delete($sql)){
                echo '删除成功';
            }
            die();
        }
        $rs = $this->home->getAllMenu();
//         print_r($rs);
        $this->assign('rs', $rs);
        
    }
    
    public function actionsAddmenu()
    {
        if (Http::get('do')){
            $res='失败';
            $saveData = array(
                    'pmodule'=>Http::post('pmodule'),
                    'module'=>Http::post('module'),
                    'action'=>Http::post('action'),
                    'name'=>Http::post('name'),
                    'pid'=>Http::post('pid'),
                    'isuse'=>Http::post('isuse'),
                    'isshow'=>Http::post('isshow'),
                    'sort'=>Http::post('sort'),
                    'target'=>Http::post('target')
                    );
            if (Http::get('do')=='add'){
                if($this->home->db->insert($this->home->menutable, $saveData)){
                    $res = '添加成功';
                    addAction(Http::post('pmodule'),Http::post('module'),Http::post('action'),Http::post('name'));
                    
                   
                }
            }
            if (Http::get('do')=='update'){
                if($this->home->db->updateData($this->home->menutable, $saveData, " WHERE id = ".Http::get('id')." ")){
                    $res  = '修改成功';
                    
                }
            }            
            die($res);
        }
        if (http::get('id')){
            $row = $this->home->getMenuById(http::get('id'));
            $this->assign('row', $row);
        }
    }
    public function actionsColumns()
    
    {
    
        if (Http::get('do')=='add'){
    
            $pid = Http::get('pid');
    
            $cname = Http::get('cname');
    
            $ename = Http::get('ename');
    
            $title= Http::get('title');
    
            $keywords = Http::get('keywords');
    
            $description = Http::get('description');
    
            $data = array('pid'=>$pid,'cname'=>$cname,'ename'=>$ename,'title'=>$title,'keywords'=>$keywords,'description'=>$description);
    
            if($this->system->addColumns($data)){
    
                echo json_encode(array('status'=>1,'html'=>'添加成功！'));
    
            }else {
    
                echo json_encode(array('status'=>0,'html'=>'添加失败，有重复栏目！'));
    
            }
    
            die();
    
        }
    
        if (Http::get('do')=='del'){
    
            $id=Http::get('id');
    
            if($this->system->delColumns($id))
    
            {
    
                echo '删除成功！';
    
            }else {
    
                echo '删除失败！';
    
            }
    
            die();
    
        }
    
        if (Http::get('do')=='modify'){
    
            $id = Http::get('id');
    
            $cname = Http::get('cname');
    
            $ename = Http::get('ename');
    
            $title= Http::get('title');
    
            $keywords = Http::get('keywords');
    
            $description = Http::get('description');
    
            $data = array('cname'=>$cname,'ename'=>$ename,'title'=>$title,'keywords'=>$keywords,'description'=>$description);
    
            if ($this->system->modifyColumns($id,$data)){
    
                echo json_encode(array('status'=>1,'html'=>'修改成功！'));
    
            }else {
    
                echo json_encode(array('status'=>0,'html'=>'修改失败，有重复栏目！'));
    
            }
    
            die();
    
        }
        
        $list = $this->system->getColumnsList();
    
        $this->assign('list', $list);
    
    }

}#classEnd